﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Applee.FractManager;
using Applee.FractCommon;

namespace WinFract
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            FractManager fractManager = FractManager.Instance;
            string[] strNames = fractManager.Initialize();

            toolStripComboBox.Items.Add("请选择...");

            foreach (string strName in strNames)
            {
                toolStripComboBox.Items.Add(strName);
            }

            toolStripComboBox.SelectedIndex = 0;
        }

        private void drawingPad_Paint(object sender, PaintEventArgs e)
        {
            Size size = drawingPad.Size;
            Rectangle rect = drawingPad.ClientRectangle;

            Graphics graphics = e.Graphics;
            Brush brush = Brushes.White;
            graphics.FillRectangle(brush, rect);

            Rectangle rectInner = rect;
            //rectInner.Inflate(-5, -5);


            graphics.DrawRectangle(Pens.Black, rectInner);
            
            FractManager fractManager = FractManager.Instance;
            IDrawable drawable = fractManager.Drawable;
            if (drawable != null)
            {
                drawable.OnPaint(e);
            }
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            RefreshFract();
            drawingPad.Refresh();
        }

        private void toolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            CreateFract();
            drawingPad.Refresh();
        }

        private void toolStripButtonOK_Click(object sender, EventArgs e)
        {
            CreateFract();
            drawingPad.Refresh();
        }

        private void CreateFract()
        {
            try
            {
                float length = Convert.ToSingle(toolStripTextBoxLength.Text);
                int iterations = Convert.ToInt32(toolStripTextBoxIterations.Text);

                FractManager fractManager = FractManager.Instance;
                fractManager.ViewSize = drawingPad.Size;

                fractManager.CreateFract((string)toolStripComboBox.SelectedItem,
                    length, iterations);

                IFractable fractable = fractManager.Drawable as IFractable;
                toolStripTextBoxLength.Text = fractable.Length.ToString();
            }
            catch (Exception)
            {
            }
        }

        private void RefreshFract()
        {
            try
            {
                float length = Convert.ToSingle(toolStripTextBoxLength.Text);
                int iterations = Convert.ToInt32(toolStripTextBoxIterations.Text);

                FractManager fractManager = FractManager.Instance;
                fractManager.ViewSize = drawingPad.Size;

                fractManager.RefreshFract((string)toolStripComboBox.SelectedItem,
                    length, iterations);

                IFractable fractable = fractManager.Drawable as IFractable;
                toolStripTextBoxLength.Text = fractable.Length.ToString();
            }
            catch (Exception)
            {
            }
        }

        private void toolStripButtonStd_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButtonZoomIn_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButtonZoomOut_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButtonMove_Click(object sender, EventArgs e)
        {
            //drawingPad.Cursor = new Cursor(Properties.Resources.OpenHand.GetHbitmap());
            //drawingPad.Cursor = Cursors.Cross;
            SetCursor(Properties.Resources.OpenHand, new Point());
        }

        public void SetCursor(Bitmap cursor, Point hotPoint)
        {
            int hotX = hotPoint.X;
            int hotY = hotPoint.Y;
            Bitmap myNewCursor = new Bitmap(cursor.Width * 2 - hotX, cursor.Height * 2 - hotY);
            Graphics g = Graphics.FromImage(myNewCursor);
            g.Clear(Color.FromArgb(0, 0, 0, 0));
            g.DrawImage(cursor, cursor.Width - hotX, cursor.Height - hotY, cursor.Width,
            cursor.Height);

            myNewCursor.MakeTransparent();
            
            drawingPad.Cursor = new Cursor(myNewCursor.GetHicon());

            g.Dispose();
            myNewCursor.Dispose();
        } 

        private void toolStripTextBoxIterations_Click(object sender, EventArgs e)
        {
            toolStripTextBoxIterations.SelectAll();
        }
    }
}

